
package com.yytek.gw.api;

import com.yytek.common.core.domain.R;
import com.yytek.common.core.constant.ValueConstant;
import com.yytek.common.core.domain.AttributeInfo;
import com.yytek.common.core.domain.CommandRequest;
import com.yytek.common.core.domain.Device;
import com.yytek.gw.service.DriverCustomService;
import com.yytek.gw.utils.DriverUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 驱动操作指令 Rest Api
 *
 * @author pnoker
 */
@Slf4j
@RestController
@RequestMapping(ValueConstant.Driver.COMMAND_URL_PREFIX)
public class DriverCommandApi {

    @Resource
    private DriverCustomService driverCommandService;
    @Value("${driver.port}")
    private Integer driverPort;
    /**
     * 读
     * @return R
     */
    @PostMapping("/read")
    public R readPoint(@RequestBody CommandRequest commandRequest) throws Exception {
        //当前驱动基本信息
        Map<String, AttributeInfo> driverInfo = DriverUtil.getBaseDriverInfo(driverPort.toString());
        Device device =new Device();
        device.setIotId(commandRequest.getDeviceId());
        return driverCommandService.read(driverInfo,commandRequest.getDataInfo(),device);
    }
    /**
     * 写
     * @return R<Boolean>
     */
    @PostMapping("/write")
    public R writePoint(@RequestBody CommandRequest commandRequest) throws Exception {
        //当前驱动基本信息
        Map<String, AttributeInfo> driverInfo = DriverUtil.getBaseDriverInfo(driverPort.toString());
        Device device =new Device();
        device.setIotId(commandRequest.getDeviceId());
        return driverCommandService.write(driverInfo,commandRequest.getDataInfo(),device);
    }
}
